package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaItemDictParam;
import com.ysd.lis.entity.sta.StaTextResult;
import com.ysd.lis.mapper.sta.StaTextResultMapper;
import com.ysd.lis.service.sta.StaTextResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_module 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaTextResultServiceImpl extends ServiceImpl<StaTextResultMapper, StaTextResult> implements StaTextResultService {

    @Autowired
    StaTextResultMapper staTextResultMapper;
    @Override
    public Result findStaTextResultList(StaItemDictParam param) {
        LambdaQueryWrapper<StaTextResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StaTextResult::getDelFlag, 0)
                .eq(StaTextResult::getItemNo, param.getItemNo())
                .orderByAsc(StaTextResult::getSeq);
        List<StaTextResult> labTextResults = staTextResultMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", labTextResults);
    }

    @Override
    public Result batchDeleteStaTextResult(List<StaTextResult> staTextResults) {
        List<String> ids = staTextResults.stream().map(StaTextResult::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result saveOrUpdateStaTextResult(List<StaTextResult> staTextResults) {
        List<StaTextResult> collect = staTextResults.stream().filter(item -> StringUtils.isNotBlank(item.getResult())).collect(Collectors.toList());

        boolean b = this.saveOrUpdateBatch(collect);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }
}
